/**
 * Application Application.java
 */

package CCT220_Aula18_Exercicio01_13106;


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula18_Exercicio01_13106
 * </br>Class: Application
 * </br>Last Change: 28/10/2011 12:11:12 by marcel
 * </br>Description:
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Application {

	private JTextField                    jtfNome;
	private JPasswordField                jpfSenha;
	private final HashMap<String, String> listaSenhas;


	public Application() {

		listaSenhas = new HashMap<String, String>();
		listaSenhas.put("user01", "pass01");
		listaSenhas.put("user02", "pass02");
		listaSenhas.put("user03", "pass03");
		listaSenhas.put("user04", "pass04");
		listaSenhas.put("user05", "pass05");
		this.start();
	}


	private void start() {

		startComponents();
		JFrame jf = new JFrame("Marcel M. Grilo - 13106");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		jf.setSize(400, 100);
		jf.setResizable(false);
		JPanel jp = new JPanel(new GridBagLayout());
		jp.setBackground(Color.WHITE);
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;

		JLabel jlInfo = new JLabel(
		        "Complete os campos e pressione enter para validar");
		jlInfo.setForeground(Color.ORANGE);
		c.gridx = 0;
		c.gridx = 0;
		c.gridwidth = 2;
		jp.add(jlInfo, c);
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		jp.add(new JLabel("Nome:  "), c);
		c.gridx = 1;
		jp.add(jtfNome, c);
		c.gridx = 0;
		c.gridy = 2;
		jp.add(new JLabel("Senha: "), c);
		c.gridx = 1;
		c.gridy = 2;
		jp.add(jpfSenha, c);
		jf.add(jp);

		jf.setVisible(true);

	}


	/**
	 * </br>******************************
	 * </br>Package: CCT220_Aula18_Exercicio01_13106
	 * </br>Class: Application
	 * </br>Method: startComponents
	 * </br>Last Change: 28/10/2011 12:19:02 by marcel
	 * </br>Description:
	 * </br>******************************</br>
	 * 
	 */
	private void startComponents() {

		jtfNome = new JTextField();
		jtfNome.setToolTipText("Pode ser: user01; user02; user03; user04; user05;");
		jtfNome.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {

			}


			@Override
			public void keyReleased(KeyEvent e) {

			}


			@Override
			public void keyPressed(KeyEvent e) {

				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					valida();
				}
			}

		});
		jpfSenha = new JPasswordField();
		jpfSenha.setToolTipText("Pode ser: pass01; pass02; pass03; pass04; pass05;");
		jpfSenha.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {

			}


			@Override
			public void keyReleased(KeyEvent e) {

			}


			@Override
			public void keyPressed(KeyEvent e) {

				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					valida();
				}
			}

		});

	}


	private void valida() {

		if (listaSenhas.containsKey(jtfNome.getText())) {
			if (listaSenhas.get(jtfNome.getText()).equals(
			        new String(jpfSenha.getPassword()))) {
				JOptionPane.showMessageDialog(null,
				        "Voce esta conectado ao sistema!!!", "Login",
				        JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "Senha incorreta", "Login",
				        JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(null, "Nome de usuario inexistente",
			        "Login", JOptionPane.ERROR_MESSAGE);
		}
	}


	public static void main(String[] args) {

		new Application();
	}
}
